<?php
namespace app\admin\controller;
use think\Db;

class Material
{
	public function __construct()
    {
		Admin_auth();//是否登录
		$config = Db::table('material_config')->where('id',1)->find();
		config([
			'UPLOAD_SITEIMG_QINIU'=>[
			'maxSize'          => 5 * 1024 * 1024,//文件大小
			'rootPath'         => './',
			'saveName'         => ['uniqid', ''],
			'driver'           => 'Qiniu',
				'driverConfig' => [
					'secretKey' => $config['secretKey'], 
					'accessKey' => $config['accessKey'],
					'domain'    => $config['domain'],
					'bucket'    => $config['bucket'],
				],
			],
			'file_size'=>$config['file_size']
		]);
    }

	public function index()
    {
		//print_r(config('UPLOAD_SITEIMG_QINIU'));exit;
		$uid = "a_".session('uid');
		$image_category = Db::table('material_category')->where(['type'=>'image','uid'=>$uid])->order('id')->select();
		$audio_category = Db::table('material_category')->where(['type'=>'audio','uid'=>$uid])->order('id')->select();
		$video_category = Db::table('material_category')->where(['type'=>'video','uid'=>$uid])->order('id')->select();
		$other_category = Db::table('material_category')->where(['type'=>'other','uid'=>$uid])->order('id')->select();
		$view = new \think\View();
		$view->assign('image_category',$image_category);
		$view->assign('audio_category',$audio_category);
		$view->assign('video_category',$video_category);
		$view->assign('other_category',$other_category);
		return $view->fetch();
    }
	
	public function addcategory()
    {
		$material_category = Db::table('material_category');
		$data['type'] = input('get.type');
		$data['title'] = input('get.title');
		$data['uid'] = "a_".session('uid');
		$catid = $material_category->insert($data);
		if($catid){
			$return = array(
				'code' => 'success',
				'msg' => '添加成功',
				'catid' => $catid
			);
			return $return;
		}
    }
	
	public function delcategory()
    {
		$uid = "a_".session('uid');
		$id = input('get.id');
		$keys = Db::table('material')->where(['cid'=>$id,'uid'=>$uid])->column('name');
		if(empty($keys)){
			Db::table('material_category')->where(['id'=>$id,'uid'=>$uid])->delete();
			$return = array(
				'code' => 'success',
				'msg' => '删除成功'
			);
			return $return;
		}else{
			$setting=config('UPLOAD_SITEIMG_QINIU');
			$Upload = new \think\Upload($setting);
			$info = $Upload->delBatch($keys);
			if($info != 'false'){
				Db::table('material')->where(['cid'=>$id,'uid'=>$uid])->delete();
				Db::table('material_category')->where(['id'=>$id,'uid'=>$uid])->delete();
				$return = array(
					'code' => 'success',
					'msg' => '删除成功'
				);
				return $return;
			}else{
				$return = array(
					'code' => 'error',
					'msg' => '删除失败'
				);
				return $return;
			}
		}
    }
	
	public function upload()
    {
		$type = input('get.type');
		$cid = input('get.cat');
		$uid = "a_".session('uid');
		$material = Db::table('material');
		$files = $_FILES['files'];
		if($type != 'other'){
			foreach($files['type'] as $file_type){
				if(strpos($file_type,$type) === false){
					$return = array(
						'code' => 'error',
						'msg' => '文件格式不正确'
					);
					return $return;
				}
			}
		}
		
		foreach($files['size'] as $file_size){
			if($file_size > config('file_size')){
				$return = array(
					'code' => 'error',
					'msg' => '文件不能大于'.(config('file_size')/1024)."Kb"
				);
				return $return;
			}
		}
		$setting=config('UPLOAD_SITEIMG_QINIU');
		$Upload = new \think\Upload($setting);
		$infos = $Upload->upload($_FILES);
		
		$i = 0;
		foreach($infos as $info){
			$data['uid'] = $uid;
			$data['cid'] = $cid;
			$data['type'] = $type;
			$data['title'] = $files['name'][$i];
			$data['name'] = str_replace("/","_",$info['name']);
			$data['ext'] = $info['ext'];
			$data['size'] = $info['size'];
			$data['md5'] = $info['md5'];
			$data['sha1'] = $info['sha1'];
			$data['url'] = $info['url'];
			$data['time'] = time();
			$material->insert($data);
			$i++;
		}
		$return = array(
			'code' => 'success',
			'msg' => '上传成功'
		);
		return $return;
    }
	
	public function del_file()
    {
		$id = input('get.id');
		$uid = "a_".session('uid');
		$key = Db::table('material')->where(['uid'=>$uid,'id'=>$id])->value('name');
		$setting=config('UPLOAD_SITEIMG_QINIU');
		$Upload = new \think\Upload($setting);
		$info = $Upload->del($key);
		if($info != 'false'){
			if(Db::table('material')->delete($id)){
				$return = array(
					'code' => 'success',
					'msg' => "删除成功"
				);
				return $return;
			}
		}
		
    }
	
	public function ajax_get()
    {
		$type = input('get.type');
		$cid = input('get.cat');
		$page = input('get.page') ? input('get.page') : 0;
		$uid = "a_".session('uid');
		$page_setting=config('page');
		if($cid==0){
			$materialdata = Db::table('material')->where(['uid'=>$uid,'type'=>$type])->page($page.",".$page_setting[$type])->order('time desc')->select();
			$count = Db::table('material')->where(['uid'=>$uid,'type'=>$type])->count();
		}else{
			$materialdata = Db::table('material')->where(['uid'=>$uid,'cid'=>$cid,'type'=>$type])->page($page.",".$page_setting[$type])->order('time desc')->select();
			$count = Db::table('material')->where(['uid'=>$uid,'cid'=>$cid,'type'=>$type])->count();
		}
		$return = array(
			'data' => $materialdata,
			'pages' => ceil($count/$page_setting[$type])
		);
		return $return;
    }

}
